package io;//: io/UsingRandomAccessFile.java

import java.io.*;

public class UsingRandomAccessFile {
    static String file = "D:\\MavenSpace\\thinkinjava\\src\\main\\java\\io\\rtest.txt";


    static void display() throws IOException {

        RandomAccessFile rf = new RandomAccessFile(file, "r");
        for (int i = 0; i < 7; i++) {
            System.out.println("Value " + i + ": " + rf.readDouble());
        }
        System.out.println(rf.readUTF());
        rf.close();
    }

    public static void main(String[] args)
            throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file, "rw");
        for (int i = 0; i < 7; i++) {
            //写入内容
            rf.writeDouble(i * 1.414);
        }
        //第8个写入字符串
        rf.writeUTF("The end of the file");
        rf.close();

        display();
        System.out.println("----------------------------------------");
        rf = new RandomAccessFile(file, "rw");
        //位置5，第6个数值被改变，一个double占8位  5*8 40位
        rf.seek(5 * 8);
        rf.writeDouble(47.0001);
        rf.close();
        display();
    }
}
/* Output:
Value 0: 0.0
Value 1: 1.414
Value 2: 2.828
Value 3: 4.242
Value 4: 5.656
Value 5: 7.069999999999999
Value 6: 8.484
The end of the file
Value 0: 0.0
Value 1: 1.414
Value 2: 2.828
Value 3: 4.242
Value 4: 5.656
Value 5: 47.0001
Value 6: 8.484
The end of the file
*///:~
